package com.dao.impl;

import com.dao.ReceivingAddressDao;
import com.pojo.ReceivingAddress;
import com.utils.JDBCUtil;

import java.util.List;


public class ReceivingAddressDaoImpl implements ReceivingAddressDao {

    /**
     *  根据用户ID查询所有地址
     * @param userID
     * @return
     */
    @Override
    public List<ReceivingAddress> selectByUserID(String userID) {
        return JDBCUtil.queryList(ReceivingAddress.class,"select * from s_receiving_address where user_id = ? order by is_default desc",userID);
    }

    /**
     * 添加地址
     * @param receivingAddress
     * @return
     */
    @Override
    public int addRA(ReceivingAddress receivingAddress) {
        return JDBCUtil.update("insert into s_receiving_address values(?,?,?,?,?,?)",
                receivingAddress.getId(),receivingAddress.getReceivingAddress(),
                receivingAddress.getReceivingPerson(),receivingAddress.getMobilePhone(),
                receivingAddress.getUserId(),receivingAddress.getIsDefault());
    }

    /**
     * 修改地址
     * @param receivingAddress
     * @return
     */
    @Override
    public int updateRA(ReceivingAddress receivingAddress) {
        return JDBCUtil.update("update s_receiving_address set receiving_address = ?,receiving_person=?,mobile_phone=?,is_default=? where id = ?",
                receivingAddress.getReceivingAddress(),receivingAddress.getReceivingPerson(),receivingAddress.getMobilePhone(),receivingAddress.getIsDefault(),receivingAddress.getId());
    }


    /**
     * 删除地址
     * @param id
     * @return
     */
    @Override
    public int deleteRA(String id) {
        return JDBCUtil.update("delete from s_receiving_address where id = ?", id);
    }

    /**
     * 根据用户ID删除地址
     * @param userID
     * @return
     */
    @Override
    public int updateRAByUserID(String userID) {
        return JDBCUtil.update("update s_receiving_address set is_default=0 where user_Id = ?",userID);
    }

    /**
     * 根据id修改
     * @param id
     * @return
     */
    @Override
    public ReceivingAddress selectByID(String id) {
        return JDBCUtil.queryOne(ReceivingAddress.class,"select * from s_receiving_address where id = ?",id);
    }
}
